﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using tracker.Models.DTO;
using tracker.Models;
using tracker.Models.DAL;
using System.Collections.Generic;

namespace tracker.Models.BLL
{
    public class EUserLoginBLL
    {
        public int login(string userName, string passWord)
        {
            // Get user from db
            UserLoginDAL userDA = new UserLoginDAL();
            EUserLogin user = userDA.getByUserName(userName);

            // MD5
            // passWord = Utilities.GetMD5Hash(passWord);
           
            // Check
            if (user == null || user.Password != Utilities.GetMD5Hash(passWord))
            {
                return 0;
            }

            // Create session
            HttpContext.Current.Session.Add(Common.USER, userName);
            HttpContext.Current.Session.Add(Common.USER_SECUTIRY_CODE, user.SecurityCode);
            HttpContext.Current.Session.Add(Common.STATUS, user.Status);
            HttpContext.Current.Session.Add(Common.USER_GROUPID, user.UserGroupID);
            HttpContext.Current.Session.Add(Common.USER_ID, user.UserID);
HttpContext.Current.Session.Add(Common.LANGUAGE, "vi");
            HttpContext.Current.Session.Add(Common.LANGUAGE, "vi");
            HttpContext.Current.Session.Add(Common.TIMEZONE, "420");
            return 5;
        }
        public int checkSession()
        {
            if (HttpContext.Current.Session[Common.USER] != null)
            {
                return 1;
            }
            return 0;
        }
        public EUserLogin getByUserName(string userName)
        {
            UserLoginDAL dl = new UserLoginDAL();
            return dl.getByUserName(userName);
        }
        public EUserLogin getBySecurityCode(string securityCode)
        {
            UserLoginDAL dl = new UserLoginDAL();
            List<EUserLogin> lstData =  dl.getUserLoginBySecurityCode(securityCode);
            if (lstData != null && lstData.Count > 0)
            {
                return lstData[0];
            }
            return null;

        }
        public bool checkExist(string userName)
        {
            if (getByUserName(userName) != null)
            {
                return true;
            }
            return false;
        }
        public String getParentCode(string userName)
        {
            EUserLogin user = getByUserName(userName);
            if (user != null)
            {
                return user.ParentCode;
            }
            return "";
        }
        public string changePassword(string securityCode, string oldPassword, string newPassword)
        {
            EUserLogin user = getBySecurityCode(securityCode);
            if (user != null && user.Password == Utilities.GetMD5Hash(oldPassword)){
            
                newPassword = Utilities.GetMD5Hash(newPassword);
                return new UserLoginDAL().updatePassword(securityCode, newPassword);
            }
            else
            {
                return "1";
            }
        }
    }
}
